import axios, { AxiosInstance } from 'axios'

import type { RequestInterceptors, RequestConfig, extraConfig } from '../type'

export class AxiosRequest {
  // axios 实例
  instance: AxiosInstance
  interceptors?: RequestInterceptors

  constructor(config?: RequestConfig, extraConfig?: extraConfig) {
    this.instance = axios.create(config)
    console.log('111')

    this.interceptors = config?.interceptors
    this.instance.interceptors.request.use(
      this.interceptors?.requestInterceptor,
      this.interceptors?.requestInterceptorCatch
    )

    this.instance.interceptors.response.use(
      this.interceptors?.responseInterceptor,
      this.interceptors?.responseInterceptorCatch
    )
  }

  // extraConfig 暂时不做处理 -- > 预留
  request<T = any>(config: RequestConfig<T>) {
    return this.instance.request<T>(config)
  }
}

export default AxiosRequest
